// pages/my/addCustomGoods/addCustomGoods.js
import * as appConfig from '../../../app-config.js'
var app = getApp()
Page({
  data: {
    specName:'',
    specId:'',
    list:[],
    specBox:[],
    verList:[],
    specNameList:[],
    specIdList:[],
    page:2,
    agentId:''
  },
  onLoad: function(options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
      this.getSpec(res.data.agentId)
    })
    
  },
  onShow(){
    this.getSpec(this.data.agentId)
    this.setData({
      page:2
    })
  },
  monitorSpecName(){
    let specBox = this.data.specBox
    let verList = this.data.verList
    let specName = ''
    let specId = ''
    let specNameList = []
    let specIdList = []
    specBox.forEach(item=>{

      if (item=='') {

      }else{ 
        if (item.length!=undefined) {

        }else{
          specName += item.versionValueName
          specId += item.versionValueId+','
        } 
      }
    })
    console.log(specId)
    //取出颜色和其他属性进行拼接
    verList.forEach(item=>{
      specNameList.push(item.versionValueName+specName)
      specIdList.push(item.versionValueId+','+specId.substr(0,specId.length-1))
    })

    this.setData({
      specName,
      specId:specId.substr(0,specId.length-1),
      specNameList,
      specIdList
    })
  },
  chooseSpec(e){
    console.log(e.currentTarget.dataset)
    const that = this
    let id = e.currentTarget.dataset.id
    let verId = e.currentTarget.dataset.verId
    let list = this.data.list
    let verList = this.data.verList
    let maoId = ''
    list.forEach((item,i)=>{
      let specList = item.specList  
      
      //判断规格类型
      if (verId==item.id) {
        //颜色多选
        if (item.id==1) {
          specList.forEach((data,j)=>{
            if (id==data.versionValueId) {
              //反选
              if (data.current) {
                data.current=false
                verList.forEach((res,x)=>{
                  if (id==res.versionValueId) {
                    verList.splice(x,1)
                  }
                })
                that.setData({
                  ['specBox['+i+']']:verList
                })
              }else{
                data.current=true
                verList.push({
                  versionValueName:data.versionValueName,
                  versionValueId:data.versionValueId
                })
                that.setData({
                  ['specBox['+i+']']:verList
                })
              }
            }else{
              // data.current=false
            }
            that.setData({
              ['list['+i+'].specList']:specList
            })
          })
            
        }else{      
          
          specList.forEach((data,j)=>{
            //判断属性
            if (id==data.versionValueId) {
              //反选
              if (data.current) {
                data.current=false
                that.setData({
                  ['specBox['+i+']']:'',
                })
              }else{
                data.current=true
                that.setData({
                  ['specBox['+i+'].versionValueName']:data.versionValueName,
                  ['specBox['+i+'].versionValueId']:data.versionValueId,
                })
              }
            }else{
              data.current=false
            }
            that.setData({
              ['list['+i+'].specList']:specList
            })
          })
        }
      }
    })
    this.monitorSpecName()
  },
  getSpec(agentId){
    var that = this
    let data = {
      pageNum:1,
      pageSize:5,
      agentId
    }
    console.log(data)
    app.request('wx_app/custom_merchandise/versionList',data,'GET').then(res=>{
      console.log(res)
      let list = []
      if (res.code==1) {
        res.data.pageContent.forEach(item=>{
          let specList = []
          item.versionValueList.forEach((data,index)=>{
            // if (index==0) {
            //   data.current=true
            // }else{
              data.current=false
            // }
            specList.push(data)
          })
          list.push({
            name:item.name,
            id:item.id,
            specList
          })
        })
        that.setData({
          list
        })
      }else{

      }
    })
  },
  loadMore(){
    var that = this
    let page = this.data.page

    let data = {
      pageNum:page,
      pageSize:10,
      agentId:this.data.agentId
    }
    console.log(data)
    app.request('wx_app/custom_merchandise/versionList',data,'GET').then(res=>{
      console.log(res)
      let list = this.data.list
      if (res.code==1) {
        res.data.pageContent.forEach(item=>{
          let specList = []
          item.versionValueList.forEach((data,index)=>{
            // if (index==0) {
            //   data.current=true
            // }else{
              data.current=false
            // }
            specList.push(data)
          })
          list.push({
            name:item.name,
            id:item.id,
            specList
          })
        })
        that.setData({
          list
        })
      }else{

      }
      page++
      this.setData({
        page
      })
    })
  },
  saveChooseSpec(){
    let id = this.data.specId
    ,name = this.data.specName
    ,nameList = this.data.specNameList
    ,idList = this.data.specIdList
    ,pages = getCurrentPages()
    ,prev = pages[pages.length - 2]
    ,versionList = prev.data.versionList
    versionList.forEach(item=>{
      idList.forEach(data=>{
        if (item.specId===data) {
            wx.showToast({
            title:'请勿重复添加',
            icon:'none'
          })
          //抛出一个异常阻止执行
          alert("请勿重复添加");
        }
      })
    })

    if (nameList=='') {
      versionList.push({
        specName:name,
        specId:id
      })
    }else{
      nameList.forEach((item,i)=>{
        idList.forEach((data,j)=>{
          if (i===j) {
            versionList.push({
              specName:item,
              specId:data
            })
          }
        })
      })
    }
    prev.setData({
      versionList
    })
    wx.navigateBack({
      delta:1
    })
  },
  addNewSpec(e){
    let id = e.currentTarget.dataset.specId
    wx.navigateTo({
      url:'addNewSpec?id='+id
    })
  },
  addNewSpecType(){
    wx.navigateTo({
      url:'addNewSpecType'
    })
  }
})

